package com.sina.shop.util;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

import static com.sina.shop.util.FileUtils.getFileExtension;


/**
 * created  on 06/06/2017.
 */
public abstract class FileUploadUtils {

    private FileUploadUtils(){

    }

    /**
     * 将文件上传到临时目录
     * @param context 可 null
     * @param multipartFile not null
     * @return 上传成功后的文件位置
     * @throws IOException - 上传失败
     */
    public static File uploadToTemp(ServletContext context, MultipartFile multipartFile) throws IOException {

        File directory = new File(System.getProperty("java.io.tmpdir"));
        String fileName ;

        fileName = UUID.randomUUID().toString()
                + getFileExtension(multipartFile.getOriginalFilename());

        File destFile = new File(directory,fileName);

        FileUtils.createFile(destFile);

        multipartFile.transferTo(destFile);
        return destFile;
    }




}
